var	httpObj;
var	timer;		//	タイムアウト用
var target_url='../systemlog/log0000';
var reloadTime=30000;	//30秒

var systemlogOption={
		"logCate" : "" //ログカテゴリー
		,"limit"  : "100" //表示件数
	};

var objType=0

function reloadLOG(){
	var tid=setTimeout("httpRequest()",reloadTime);
}

//*==============================================//
//*	関数：httpRequest()		  *//
//	内容：XML読み取り開始		  *//
//*==============================================//
function httpRequest()
{
	try
	{
		if(window.ActiveXObject){
			objType=0;
			httpObj = new ActiveXObject("Microsoft.XMLHTTP");
		}else if(window.XMLHttpRequest){
			objType=1
			httpObj = new XMLHttpRequest();
		}else{
			//エラー
			return;
		}
	}
	catch(e)
	{
		//エラー
		return;
	}
	//	タイマーセット（タイムアウトエラー用）
	timer = setInterval("timeoutError()",60000); //60秒にセット
//	timer = setInterval("timeoutError()",7200000); //2時間にセット（バックアップ処理が存在するから）

	//	データを取得する
	var param="";
	if (systemlogOption) {
		param="?logCate="+systemlogOption.logCate;
		param+="&limit="+systemlogOption.limit;
	}
	
	httpObj.open("GET", target_url+param, true);
	httpObj.onreadystatechange = DataRead;
	httpObj.send("");
	reloadLOG();  //自動更新を行う場合はコメントをはずす
	return false;
}

//*==============================================//
//*	関数：timeoutError()		  *//
//	内容：タイムアウト			  *//
//*==============================================//
function timeoutError()
{
	clearInterval(timer);	//	タイマーとめる
	httpObj.abort();
	//タイムアウト
}

//*==============================================//
//*	関数：DataRead()			  *//
//	内容：XML読み取ったあと		  *//
//*==============================================//
function DataRead()
{
	if ( httpObj.readyState == 4 ){
		clearInterval(timer);	//	タイマーとめる
		if ( httpObj.status == 200){
			DataOut();
		}
	}
}

//*==============================================//
//*	関数：DataOut()			  *//
//	内容：書き出し			  *//
//*==============================================//
function DataOut()
{
			//	返り値ＸＭＬの取得
	var xtree = httpObj.responseXML;
			//	ログリストの取得
	var loglist = xtree.getElementsByTagName("item");
			//	書き出し
	var buf = "";
	for(var i = 0 ; i < loglist.length ; i ++ )
	{
		//	１件分のログデータ取り出し
		lognode	= 	loglist[i];
		//	各種データ取り出し
		
		var log_id = getValue(lognode,"log_id");
		var log_cate = getValue(lognode,"log_cate");
		var log_topic = getValue(lognode,"log_topic");
		var log_text = getValue(lognode,"log_text");
		var log_date = getValue(lognode,"log_date");
		var hint_msg = getValue(lognode,"hint_msg");
		var link_url = getValue(lognode,"link_url");
		var link_key = getValue(lognode,"link_key");
		var error_cnt = getValue(lognode,"error_cnt");
		var record_cnt = getValue(lognode,"record_cnt");
		var alert_cnt = getValue(lognode,"alert_cnt");
		var logColor = "#0000ff";
		var errMsg = "正常";
		if (error_cnt != "0"){
			logColor = "#ff0000";
			errMsg = "異常" + error_cnt + "件";
		}else if (alert_cnt != "0") {
			logColor = "#EF9500";
			errMsg = "警告" + alert_cnt + "件";
		}
		var logUrl = "";
		if (link_url != ""){
			logUrl = link_url;
			if (link_key != ""){
				logUrl = logUrl + "?" + "parentId="+log_id;
			}
			logUrl = "onClick=\"logSubOpen('" + logUrl + "');\"";
		}
		//	書き出し
		buf = buf + "<span style=\"color:" + logColor + ";cursor:pointer;\" " + logUrl + ">" + log_date + "　" + errMsg + "　" + log_topic + "　" + log_text + "　" + hint_msg + "</span><br>";
	}
	document.getElementById("divOutput").innerHTML = buf;

}

function getValue(lognode,name){
	var retValue="";
	if(objType==0){
		retValue=lognode.selectSingleNode(name).nodeTypedValue;
	}else{
		retValue = lognode.getElementsByTagName(name)[0].firstChild.nodeValue;
	}
	return retValue;
}

function logSubOpen(logUrl)
{
	var features="location=no, menubar=no, status=no, scrollbars=no, resizable=yes, toolbar=no";
	var width = 1024;
	var height = 710;

	if (width) {
		if (window.screen.width > width)
			features+=", left="+(window.screen.width-width)/2;
		else width=window.screen.width;
		features+=", width="+width;
	}

	if (height) {
		if (window.screen.height-50 > height)
			features+=", top="+((window.screen.height-50) -height)/2;
		else height=window.screen.height;
		features+=", height="+height;
	}

	window.open(logUrl,null,features);
}
